package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.blankj.utilcode.util.ClipboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.TextAdapter;
import flc.ast.databinding.ActivitySearchResultBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.api.StkApiRet;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.utils.IntentUtil;
import stark.common.bean.StkResBean;
import stark.common.bean.StkResSetBean;

public class SearchResultActivity extends BaseAc<ActivitySearchResultBinding> {

    public static String key;
    private TextAdapter searchAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_search_result;
    }

    @Override
    protected void initView() {
        mDataBinding.rvSearchList.setLayoutManager(new LinearLayoutManager(mContext));
        searchAdapter = new TextAdapter();
        mDataBinding.rvSearchList.setAdapter(searchAdapter);
        searchAdapter.addChildClickViewIds(R.id.ivTextItemCopy);
        searchAdapter.addChildClickViewIds(R.id.ivTextItemShare);
        searchAdapter.setOnItemChildClickListener(this);
        searchAdapter.setOnItemClickListener(this);

        mDataBinding.etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                key = mDataBinding.etSearch.getText().toString().trim();
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //先隐藏键盘
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    if (key.isEmpty()) {
                        ToastUtils.showShort(getString(R.string.please_input_text));
                        return true;
                    }
                    getData();
                    saveRecord();
                }
                return false;
            }
        });
    }

    @Override
    protected void initData() {
        mDataBinding.etSearch.setText(key);
        getData();
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        TextActivity.stkResBean = bean;
        startActivity(TextActivity.class);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivTextItemCopy://复制
                ClipboardUtils.copyText(searchAdapter.getItem(position).getDesc());
                ToastUtils.showShort(getString(R.string.copy_success_tips));
                break;
            case R.id.ivTextItemShare://分享
                IntentUtil.shareText(mContext, searchAdapter.getItem(position).getDesc());
                break;
        }
    }

    private void getData() {
        StkResApi.getTagResourceList(null, AppConfig.SEARCH_DATA + key, StkResApi.createParamMap(0, 999), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result.size() > 0) {
                        searchAdapter.setList(result);
                        mDataBinding.rvSearchList.setVisibility(View.VISIBLE);
                        mDataBinding.tvSearchNoData.setVisibility(View.GONE);
                    } else {
                        mDataBinding.rvSearchList.setVisibility(View.GONE);
                        mDataBinding.tvSearchNoData.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
    }

    private void saveRecord() {
        List<String> listSave = new ArrayList<>();
        listSave.add(key);
        List<String> listSp = AppSpUtils.getSearchRecordList();
        if (listSp != null && listSp.size() > 0) {
            listSp.remove(key);
            listSp.add(0, key);
            AppSpUtils.saveSearchRecordList(listSp);
        } else {
            AppSpUtils.saveSearchRecordList(listSave);
        }
    }

}